#include "clickedlabel.h"

#include <QMouseEvent>

ClickedLabel::ClickedLabel(QWidget *parent)
    : QLabel(parent)
    , _curState(ClickLbState::Normal)
{
    this->setCursor(Qt::PointingHandCursor); // 移动到标签上，鼠标变成小手👆
}

ClickedLabel::~ClickedLabel()
{

}

void ClickedLabel::SetState(QString normal, QString normal_hover, QString normal_press, QString selected, QString selected_hover, QString selected_press)
{
    _normal = normal;
    _normal_hover = normal_hover;
    _normal_press = normal_press;

    _selected = selected;
    _selected_hover = selected_hover;
    _selected_press = selected_press;

    setProperty("state", normal);
    repolish(this);
}

ClickLbState ClickedLabel::GetCurState()
{
    return _curState;
}

bool ClickedLabel::SetCurState(ClickLbState state)
{
    _curState = state;
        if (_curState == ClickLbState::Normal) {
            setProperty("state", _normal);
            repolish(this);
        }
        else if (_curState == ClickLbState::Selected) {
            setProperty("state", _selected);
            repolish(this);
        }

        return true;
}

void ClickedLabel::ResetNormalState()
{
    _curState = ClickLbState::Normal;
    setProperty("state", _normal);
    repolish(this);
}

void ClickedLabel::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (_curState == ClickLbState::Normal) {
//            qDebug() << "ClickedLabel clicked, change to selected press: " << _selected_press;
            _curState = ClickLbState::Selected;
            setProperty("state", _selected_press);
            repolish(this);
            update();
        } else {
//            qDebug() << "ClickedLabel clicked, change to normal press: " << _normal_press;
            _curState = ClickLbState::Normal;
            setProperty("state", _normal_press);
            repolish(this);
            update();
        }
//        emit clicked();
    }
    QLabel::mousePressEvent(event);
}

void ClickedLabel::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (_curState == ClickLbState::Normal) {
            qDebug() << "ClickedLabel released, change to normal hover: " << _selected_hover;
//            _curState = ClickLbState::Selected;
            setProperty("state", _normal_hover);
            repolish(this);
            update();
        } else {
            qDebug() << "ClickedLabel released, change to selected hover: " << _normal_hover;
//            _curState = ClickLbState::Normal;
            setProperty("state", _selected_hover);
            repolish(this);
            update();
        }
        emit clicked(this->text(), _curState);
    }
    QLabel::mousePressEvent(event);
}

void ClickedLabel::enterEvent(QEvent *event)
{
    if (_curState == ClickLbState::Normal) {
//        qDebug() << "ClickedLabel enter, change to normal hover: " << _normal_hover;
        _curState = ClickLbState::Normal;
        setProperty("state", _normal_hover);
        repolish(this);
        update();
    } else {
//        qDebug() << "ClickedLabel enter, change to selected hover: " << _selected_hover;
        _curState = ClickLbState::Selected;
        setProperty("state", _selected_hover);
        repolish(this);
        update();
    }
    QLabel::enterEvent(event);
}

void ClickedLabel::leaveEvent(QEvent *event)
{
    if (_curState == ClickLbState::Normal) {
//        qDebug() << "ClickedLabel leave, change to normal hover: " << _normal;
        _curState = ClickLbState::Normal;
        setProperty("state", _normal);
        repolish(this);
        update();
    } else {
//        qDebug() << "ClickedLabel leave, change to selected hover: " << _selected;
        _curState = ClickLbState::Selected;
        setProperty("state", _selected);
        repolish(this);
        update();
    }
    QLabel::enterEvent(event);
}
